
// HookDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HookMain.h"
#include "HookDlg.h"
#include "afxdialogex.h"
#include <atlimage.h>
#include <Strsafe.h>

#include "strfunc.h"
#include "MouseHook.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// mouse event.
static UINT UWM_NCMOUSEDBCLICK = ::RegisterWindowMessage(UWM_NCMOUSELDBCLICK_MSG);
static UINT UWM_NCLBUTTONUP = ::RegisterWindowMessage(UWM_NCMOUSELBUTTONUP_MSG);;
static UINT UWM_NCLBUTTONDOWN = ::RegisterWindowMessage(UWM_NCMOUSELBUTTONDOWN_MSG);
static UINT UWM_NCRBUTTONUP = ::RegisterWindowMessage(UWM_NCMOUSERBUTTONUP_MSG);
static UINT UWM_NCRBUTTONDOWN = ::RegisterWindowMessage(UWM_NCMOUSERBUTTONDOWN_MSG);
static UINT UWM_NCMOUSEMOVE = ::RegisterWindowMessage(UWM_NCMOUSEMOVE_MSG);

static UINT UWM_MOUSEDBCLICK = ::RegisterWindowMessage( UWM_MOUSELDBCLICK_MSG );
static UINT UWM_LBUTTONUP = ::RegisterWindowMessage(UWM_MOUSELBUTTONUP_MSG);;
static UINT UWM_LBUTTONDOWN = ::RegisterWindowMessage(UWM_MOUSELBUTTONDOWN_MSG);
static UINT UWM_RBUTTONUP = ::RegisterWindowMessage(UWM_MOUSERBUTTONUP_MSG);
static UINT UWM_RBUTTONDOWN = ::RegisterWindowMessage(UWM_MOUSERBUTTONDOWN_MSG);
static UINT UWM_MOUSEMOVE = ::RegisterWindowMessage(UWM_MOUSEMOVE_MSG);

// CHookDlg dialog
CHookDlg::CHookDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CHookDlg::IDD, pParent)
	, m_timeStartHook(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_MSG, m_listBoxMsg);
}

BEGIN_MESSAGE_MAP(CHookDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_HOOK, &CHookDlg::OnBnClickedButtonHook)
	ON_BN_CLICKED(IDC_BUTTON_UNHOOK, &CHookDlg::OnBnClickedButtonUnhook)
	ON_REGISTERED_MESSAGE(UWM_NCMOUSEMOVE, OnMyNcMouseMove)
	ON_REGISTERED_MESSAGE(UWM_NCMOUSEDBCLICK, OnMyNcMouseLDBClick)
	ON_REGISTERED_MESSAGE(UWM_NCLBUTTONUP, OnMyNcMouseLButtonUp)
	ON_REGISTERED_MESSAGE(UWM_NCLBUTTONDOWN, OnMyNcMouseLButtonDown)
	ON_REGISTERED_MESSAGE(UWM_NCRBUTTONUP, OnMyNcMouseRButtonUp)
	ON_REGISTERED_MESSAGE(UWM_NCRBUTTONDOWN, OnMyNcMouseRButtonDown)
	ON_REGISTERED_MESSAGE(UWM_MOUSEMOVE, OnMyMouseMove)
	ON_REGISTERED_MESSAGE(UWM_MOUSEDBCLICK, OnMyMouseLDBClick)
	ON_REGISTERED_MESSAGE(UWM_LBUTTONUP, OnMyMouseLButtonUp)
	ON_REGISTERED_MESSAGE(UWM_LBUTTONDOWN, OnMyMouseLButtonDown)
	ON_REGISTERED_MESSAGE(UWM_RBUTTONUP, OnMyMouseRButtonUp)
	ON_REGISTERED_MESSAGE(UWM_RBUTTONDOWN, OnMyMouseRButtonDown)
	ON_WM_NCDESTROY()
END_MESSAGE_MAP()


// CHookDlg message handlers

BOOL CHookDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHookDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHookDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CHookDlg::LogMouseMsg(UINT nMsg, WPARAM wParam, LPARAM lParam, DWORD timeSpan)
{
	wchar_t* szMSG = L"";
	switch(nMsg)
	{
	case WM_NCLBUTTONDBLCLK:	szMSG = L"WM_NCLBUTTONDBLCLK"; break;
	case WM_NCMOUSEMOVE:		szMSG = L"WM_NCMOUSEMOVE"; break;
	case WM_NCLBUTTONDOWN:		szMSG = L"WM_NCLBUTTONDOWN"; break;
	case WM_NCLBUTTONUP:		szMSG = L"WM_NCLBUTTONUP"; break;
	case WM_NCRBUTTONDOWN:		szMSG = L"WM_NCRBUTTONDOWN"; break;
	case WM_NCRBUTTONUP:		szMSG = L"WM_NCRBUTTONUP"; break;

	case WM_LBUTTONDBLCLK:		szMSG = L"WM_LBUTTONDBLCLK"; break;
	case WM_MOUSEMOVE:			szMSG = L"WM_MOUSEMOVE"; break;
	case WM_LBUTTONDOWN:		szMSG = L"WM_LBUTTONDOWN"; break;
	case WM_LBUTTONUP:			szMSG = L"WM_LBUTTONUP"; break;
	case WM_RBUTTONDOWN:		szMSG = L"WM_RBUTTONDOWN"; break;
	case WM_RBUTTONUP:			szMSG = L"WM_RBUTTONUP"; break;
	default:
		break;
	}

	POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
	int nIndex = m_listBoxMsg.AddString(WCSFMT<128>(L"[%08d] %s : %d, %d", (DWORD)timeSpan, szMSG, pt.x, pt.y));
	m_listBoxMsg.SetCurSel(nIndex);
}

void CHookDlg::OnBnClickedButtonHook()
{
	//
	m_timeStartHook = ::GetTickCount();
	m_listMsg.clear();

	m_listBoxMsg.ResetContent();

	//
	InstallMouseHook( m_hWnd );
}

void CHookDlg::OnBnClickedButtonUnhook()
{
	//
	m_timeStartHook = 0;

	UnInstallMouseHook( m_hWnd );
}

// Mouse and keyBoard Handling Functions
LRESULT CHookDlg::OnMyMouseMove(WPARAM, LPARAM param)
{

	return 1;
}

LRESULT CHookDlg::OnMyMouseLButtonUp(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_LBUTTONUP, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyMouseLButtonDown(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_LBUTTONDOWN, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyMouseRButtonUp(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_RBUTTONUP, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyMouseRButtonDown(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_RBUTTONDOWN, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);
	
	return 1;
}

LRESULT CHookDlg::OnMyMouseLDBClick(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_LBUTTONDBLCLK, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

// Mouse and keyBoard Handling Functions
LRESULT CHookDlg::OnMyNcMouseMove(WPARAM, LPARAM param)
{

	return 1;
}

LRESULT CHookDlg::OnMyNcMouseLButtonUp(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_NCLBUTTONUP, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyNcMouseLButtonDown(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_NCLBUTTONDOWN, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyNcMouseRButtonUp(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_NCRBUTTONUP, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

LRESULT CHookDlg::OnMyNcMouseRButtonDown(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_NCRBUTTONDOWN, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);
	
	return 1;
}

LRESULT CHookDlg::OnMyNcMouseLDBClick(WPARAM wParam, LPARAM lParam)
{
	//
	DWORD timeSpan = ::GetTickCount() - m_timeStartHook;

	//
	CMsg msgCur(WM_NCLBUTTONDBLCLK, wParam, lParam, timeSpan);

	//
	LogMouseMsg(msgCur.m_nMessage, msgCur.m_wParam, msgCur.m_lParam, msgCur.m_timeSpan);

	//
	m_listMsg.push_back(msgCur);

	return 1;
}

void CHookDlg::OnNcDestroy()
{
	CDialogEx::OnNcDestroy();

	if(m_timeStartHook)
	{
		OnBnClickedButtonUnhook();
	}
}
